package com.starleague.battletech.unit.jpa.appengine;

import java.util.List;
import java.util.Map;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.MappedSuperclass;

import com.google.common.collect.ImmutableList;

import com.starleague.battletech.unit.IBattletechUnitComponent;
import com.starleague.battletech.unit.IWeight;
import com.starleague.jpa.appengine.GaeDao;

@MappedSuperclass
public abstract class BattletechUnitComponentGae
        // TODO Move MechCoreComponents onto the Mech instead of HitLocations?
        // <BATTLETECH_UNIT extends BattletechUnitGae>
        extends GaeDao
        // TODO: GaeDaoChild<BATTLETECH_UNIT>
        implements IBattletechUnitComponent
{
    private static final List<? extends BattletechUnitCriticalSlotGae> NO_SLOTS = ImmutableList.of();

    @Basic(optional = false)
    @Column(nullable = false)
    @Embedded
    private WeightGae weight;

    protected BattletechUnitComponentGae(IWeight weight)
    {
        this.weight = new WeightGae(weight);
    }

    protected BattletechUnitComponentGae()
    {
        //
    }

    protected List<? extends BattletechUnitCriticalSlotGae> getCriticalSlots()
    {
        return NO_SLOTS;
    }

    @Override
    public int numHits()
    {
        int numHits = 0;
        for (BattletechUnitCriticalSlotGae criticalSlot : getCriticalSlots())
        {
            if (criticalSlot.isHit())
                ++numHits;
        }
        return numHits;
    }

    @Override
    public WeightGae getWeight()
    {
        return this.weight;
    }

    @Override
    public Map<String, Object> infoProperties()
    {
        Map<String, Object> infoPropertyMap = super.infoProperties();
        infoPropertyMap.put("weight", this.weight);
        infoPropertyMap.put("isHittable", this.isHittable());
        infoPropertyMap.put("criticalSlots", this.getCriticalSlots());
        return infoPropertyMap;
    }
}
